home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Commodities / CxCommander / CxCommander.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  5KB  |  215 lines

  1. /*
  2. **    CxComander.c by Miloslaw "Thorgal" Smyk
  3. **
  4. **    Loosely based on CxKiller by Gael Marziou.
  5. **    This program ables you to send CxMessages to
  6. **    specified Commodities.
  7. **
  8. **    Don't hesitate to use this code as you wish.
  9. **    (and please be overlooking with bugs :-) )
  10. **
  11. **  Coded on Sunday, 22 May 1994.
  12. */
  13.  
  14. #define VERSION "1.0 (22.5.94)"
  15.  
  16. #include <exec/memory.h>
  17. #include <proto/dos.h>
  18. #include <proto/commodities.h>
  19. #include <proto/exec.h>
  20.  
  21. #include <string.h>
  22.  
  23. /*** private functions of "commodities.library" ***/
  24.  
  25. #pragma libcall CxBase FindBroker 6c 801
  26. #pragma libcall CxBase CopyBrokerList ba 801
  27. #pragma libcall CxBase FreeBrokerList c0 801
  28. #pragma libcall CxBase BrokerCommand c6 802
  29.  
  30. CxObj *FindBroker(char *);
  31. LONG CopyBrokerList(struct List *);
  32. LONG FreeBrokerList(struct List *);
  33. LONG BrokerCommand(char *, LONG id);
  34.  
  35. /*** private structures & defines ***/
  36.  
  37. struct BrokerCopy    {
  38.     struct Node    bc_Node;
  39.     char    bc_Name[CBD_NAMELEN];
  40.     char    bc_Title[CBD_TITLELEN];
  41.     char    bc_Descr[CBD_DESCRLEN];
  42.     LONG    bc_Task;
  43.     LONG    bc_Dummy1;
  44.     LONG    bc_Dummy2;
  45.     UWORD    bc_Flags;
  46. };
  47.  
  48. #define COF_ACTIVE 2
  49.  
  50.  
  51. /*** prototyping made easy ***/
  52.  
  53. void TellAllBrokers(LONG);
  54. void TellOneBroker(char *, LONG);
  55.  
  56. #define TEMPLATE "CXCOMMAND,CXNAME/M,ALL/S,SHOW/S,HELP/S"
  57. #define OPT_COMMAND    0
  58. #define OPT_NAME        1
  59. #define OPT_ALL            2
  60. #define OPT_SHOW        3
  61. #define    OPT_HELP        4
  62. #define OPT_COUNT        5
  63.  
  64. char HelpString[]="\x1b[41m\x1b[32mCxCommander " VERSION " by W.F.M.H.\n\
  65. \x1b[0m\x1b[1m\x1b[33mWritten by Miloslaw 'Thorgal' Smyk\n\
  66. \x1b[0mProvides you with convinient way of sending CxMessages to Commodities\n\
  67. Recognized commands are:\nDISABLE, ENABLE, APPEAR, DISAPPEAR, KILL.\n\
  68. Commodities' names are case-sensitive!. Use SHOW to list them.\n";
  69.  
  70. const UBYTE *ver= "$VER: CxCommander " VERSION "";
  71.  
  72. const char *comms= "disableenableappeardisappearkill";
  73. /*                  0123456789 1113151719212325272931
  74.    got it?                    1012141618202224262830
  75. */
  76.  
  77. LONG RC;
  78.  
  79. struct Library *CxBase;
  80. struct DosLibrary *DOSBase;
  81.  
  82. int start(void)
  83. {
  84.   struct RDArgs *rdargs;
  85.   LONG opts[OPT_COUNT];
  86.     LONG command;
  87.     char ** argptr;
  88.  
  89. /* let's open libraries, as there is no one to do that for us. */
  90.  
  91.     if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library", 37L))
  92.     {
  93.         if(CxBase=OpenLibrary("commodities.library", 37L))
  94.         {
  95.             memset(opts, 0, sizeof(opts));
  96.  
  97. /* but command line parsing is OS stuff */
  98.             if(rdargs=ReadArgs(TEMPLATE, opts, NULL) )
  99.             {
  100. /* does user need help... */
  101.                 if(opts[OPT_HELP])
  102.                     Write(Output(), HelpString, sizeof(HelpString));
  103.                 else
  104.  
  105. /* ...or wants to see present Commodities? */
  106.                     if(opts[OPT_SHOW])
  107.                         TellAllBrokers(NULL);
  108.                     else
  109.                     if(opts[OPT_NAME] || opts[OPT_ALL])
  110.                     {
  111.  
  112. /* I wanted to have command recognition this fancy way and please don't
  113. change that. */
  114.                         switch(strstr(comms, strlwr((char *)opts[OPT_COMMAND]))-comms)
  115.                         {
  116.                             case 0:
  117.                                 command=CXCMD_DISABLE;
  118.                                 break;
  119.                             case 7:
  120.                                 command=CXCMD_ENABLE;
  121.                                 break;
  122.                             case 13:
  123.                                 command=CXCMD_APPEAR;
  124.                                 break;
  125.                             case 19:
  126.                                 command=CXCMD_DISAPPEAR;
  127.                                 break;
  128.                             case 28:
  129.                                 command=CXCMD_KILL;
  130.                                 break;
  131.                             default:
  132.                                 command=NULL;
  133.                                 break;
  134.                         }
  135.  
  136.                         argptr=(char **)opts[OPT_NAME];
  137.  
  138.                         if(command!=NULL)
  139.                             if(opts[OPT_ALL])
  140.  
  141. /* it's always nice to use self-commenting function names */
  142.                                 TellAllBrokers(command);
  143.                             else
  144.                                 while(*argptr)
  145.                                     if(BrokerCommand(*(argptr++), command))
  146.                                         RC=RETURN_WARN;
  147.                         else
  148.                             RC=RETURN_ERROR;
  149.                     }
  150.                     else
  151.                         RC=RETURN_ERROR;
  152.  
  153. /* let's clean up after ourselves */
  154.             FreeArgs(rdargs);
  155.             }
  156.             else
  157.             {
  158.                 PrintFault(IoErr(), NULL);
  159.                 RC=RETURN_FAIL;
  160.             }
  161.             CloseLibrary(CxBase);
  162.         }
  163.         else
  164.             RC=RETURN_FAIL;
  165.         CloseLibrary((struct Library *)DOSBase);
  166.     }
  167.     else
  168.         RC=RETURN_FAIL;
  169.  
  170.     return(RC);
  171. }
  172.  
  173. /* this routine gets list of available brokers and shows or signals them
  174. depending on the argument passed */
  175.  
  176. void TellAllBrokers (LONG command)
  177. {
  178.     struct List *l;
  179.     struct Node *n, *nn;
  180.  
  181.     if (l = AllocMem(sizeof(struct List), MEMF_PUBLIC))
  182.     {
  183.         /*** generate an empty list ***/
  184.         NewList(l);
  185.  
  186.         /*** private function to generate the system broker list ***/
  187.         CopyBrokerList(l);
  188.         
  189.         /*** start at head of list ***/
  190.         n = l->lh_Head;
  191.  
  192.         if(!n)
  193.             RC=RETURN_WARN;
  194.         else
  195.             while (n && (nn = n->ln_Succ))
  196.             {
  197.                 /*** send command to current broker ***/
  198.                 if(command)
  199.                     BrokerCommand((char *)&((struct BrokerCopy *)n)->bc_Name, command);
  200.                 else
  201.                 {
  202.                     Write(Output(), (char *)&((struct BrokerCopy *)n)->bc_Name, strlen((char *)&((struct BrokerCopy *)n)->bc_Name));
  203.                     Write(Output(), "\n", 1);
  204.                 }
  205.  
  206.                 /*** next node ***/
  207.                 n = nn;
  208.             }
  209.  
  210.         /*** free list and allocated mem ***/
  211.         FreeBrokerList(l);
  212.         FreeMem(l, sizeof(struct List));
  213.     }
  214. }
  215.